package app.auto.runner.base.v7tool;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.aliang.auto.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.action.ViewInflater;
import app.auto.runner.base.intf.MapBuilder;
import app.auto.runner.base.utility.ScreenSchema;

/***
 * 弹框封装类
 * @author Administrator
 *
 */
public class DialogUtilv7 {

    private static AlertDialog dialog;
    private static Dialog currentDialog;

    public static AlertDialog getCurrentDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.dismiss();
            dialog.cancel();
            dialog = null;
        }
        return dialog;
    }


    public static void cancelDialog() {
        Dialog dialog = getCurrentDialog();
        if (dialog != null) {
            dialog.dismiss();
            dialog.cancel();
            dialog = null;
        }
    }

    @SuppressLint("NewApi")
    public static Dialog showDialog(String title, View view, int linecolor, List<String> liststring, AdapterView.OnItemClickListener onitemclicklistener, int str_r_type_attr) {
        return showDialog(title, view, linecolor, liststring, onitemclicklistener, str_r_type_attr, R.layout.auto_string_item_choosable);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static Dialog showDialog(String title, View view, int linecolor, List<String> liststring, AdapterView.OnItemClickListener onitemclicklistener, int str_r_type_attr, int item_str_r_type_attr) {
        final DialogUtilv7.DialogInfo du = new DialogUtilv7.DialogInfo(view.getContext());
        du.aty = view.getContext();
        Context ctx = view.getContext();
        ScreenSchema.init(ctx);
        du.view = ViewInflater.from(ctx).inflate(str_r_type_attr, null);
        AdapterView listview = (AdapterView) du.view.findViewById(R.id.listview);
        String big = "";
        for (String s : liststring) {
            big += ",[" + s +
                    "]";
        }

        big = big.substring(1);
        listview.setTag("fake:[" +
                big + "];layout:");
        MapViewConf.with(view.getContext()).source(liststring
                , listview).conf(MapViewConf.with(view.getContext()).source(item_str_r_type_attr == -1 ? R.layout.auto_string_item_choosable : item_str_r_type_attr)).toView();
        du.neutralButtonClickListener = new DialogUtilv7.DialogClicker();
        listview.setOnItemClickListener(onitemclicklistener);
        View header = du.view.findViewById(R.id.string);
        if (header != null && ((TextView) header.findViewById(R.id.name)) != null) {
            ((TextView) header.findViewById(R.id.name)).setTextColor(ctx.getColor(android.R.color.white));
            header.setBackgroundColor(ctx.getColor(R.color.theme_color));
            if (!TextUtils.isEmpty(title)) {
                ((TextView) header.findViewById(R.id.name)).setText(title);
            }
        }
        if (dialog != null && dialog.isShowing()) {
            DialogUtilv7.cancelDialog();
        }
        dialog = DialogUtilv7.showNeutralDialog(du, true);


        setCurrentDialog(dialog);
        return dialog;
    }

    public static Dialog showSelectionDialog(final View view, int linecolor, String selecteditems, List<String> liststring, AdapterView.OnItemClickListener onitemclicklistener) {
        DialogUtilv7.DialogInfo du = new DialogUtilv7.DialogInfo(view.getContext());
        ListView listview;
        View layoutview = null;
        du.aty = view.getContext();
        du.view = layoutview = LayoutInflater.from(view.getContext()).inflate(R.layout.listview_bottom_text_center, null);
        listview = (ListView) layoutview.findViewById(R.id.listview);

        listview.setBackgroundResource(linecolor);
        String big = "";
        final List<Map> multichoose = new ArrayList<Map>();
        for (int i = 0; i < liststring.size(); i++) {
            String s = liststring.get(i);
            big += ",[" + (s + (selecteditems.contains(s) ? ",mipmap.ok" : ",mipmap.ok:gone")) +
                    "]";
            multichoose.add(MapBuilder.build().add("name", s).add("ok", "" + selecteditems.contains(s)).get());

        }
        listview.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return multichoose.size();
            }

            @Override
            public Map getItem(int i) {
                return multichoose.get(i);
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View convert, ViewGroup viewGroup) {
                if (convert == null) {
                    convert = LayoutInflater.from(view.getContext()).inflate(R.layout.auto_string_item_choosable, null);
                }
                ((TextView) convert.findViewById(R.id.name)).setText(getItem(i).get("name").toString());
                (convert.findViewById(R.id.ok)).setVisibility(getItem(i).get("ok").equals("true") ? View.VISIBLE : View.GONE);

                return convert;
            }
        });

        big = big.substring(1);
        listview.setTag("fake:[" +
                big + "];layout:auto_string_item_choosable");
//        MapViewConf.with(view.getContext()).source(liststring
//                , listview).conf(MapViewConf.with(view.getContext()).source(R.layout.auto_string_item_choosable)).toView();
        du.neutralButtonClickListener = new DialogUtilv7.DialogClicker();
        listview.setOnItemClickListener(onitemclicklistener);
        if (dialog != null && dialog.isShowing()) {
            DialogUtilv7.cancelDialog();
        }
        dialog = DialogUtilv7.showNeutralDialog(du, true);
        setCurrentDialog(dialog);
        return dialog;
    }

    public static Dialog showDialog(View view, View listview, DialogInterface.OnClickListener confirmlistener) {
        return showDialog(view.getContext(), listview, confirmlistener);
    }

    public static Dialog showDialog(Context ctx, View listview, DialogInterface.OnClickListener confirmlistener) {
        DialogUtilv7.DialogInfo du = new DialogUtilv7.DialogInfo(ctx);
        du.aty = ctx;
//        ViewGroup.MarginLayoutParams mps = new ViewGroup.MarginLayoutParams(-1,-1);
//        mps.bottomMargin = 10;
//        mps.leftMargin = 10;
//        mps.rightMargin = 10;
//        mps.topMargin = 10;
//        listview.setLayoutParams(mps);
        du.view = listview;
        if (confirmlistener == null) {
//            du.neutralButtonClickListener = new DialogUtilv7.DialogClicker();
            return DialogUtilv7.showNeutralDialog(du, true);
        } else {
            du.negativeButtonClickListener = new DialogClicker() {
                @Override
                public void onClick(DialogInterface dialog, int arg1) {
                    super.onClick(dialog, arg1);
                    DialogUtilv7.cancelDialog();
                }
            };
            du.neutralButtonText = "OK";
            du.neutralButtonClickListener = confirmlistener;
            Dialog d =
                    setCurrentDialog(DialogUtilv7.showNeutralDialog(du, true));
            d.getWindow().getDecorView().setBackgroundColor(Color.WHITE);

            return d;

        }
    }

    public static Dialog showChoiceDialog(Context ctx, View listview, String posi, DialogInterface.OnClickListener confirmlistener, String nega, DialogInterface.OnClickListener cancellistener) {
        DialogUtilv7.DialogInfo du = new DialogUtilv7.DialogInfo(ctx);
        du.aty = ctx;
//        ViewGroup.MarginLayoutParams mps = new ViewGroup.MarginLayoutParams(-1,-1);
//        mps.bottomMargin = 10;
//        mps.leftMargin = 10;
//        mps.rightMargin = 10;
//        mps.topMargin = 10;
//        listview.setLayoutParams(mps);
        du.view = listview;
        if (confirmlistener == null) {
            du.positiveButtonClickListener = new DialogUtilv7.DialogClicker();
        } else {
            du.positiveButtonText = posi;
            du.positiveButtonClickListener = confirmlistener;
        }
        if (cancellistener == null) {
            du.negativeButtonClickListener = new DialogUtilv7.DialogClicker();
        } else {
            du.negativeButtonText = nega;
            du.negativeButtonClickListener = cancellistener;
        }
        Dialog d =
                setCurrentDialog(DialogUtilv7.showChoiceDialog(du, true));
        d.getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        return d;

    }

    public static Dialog setCurrentDialog(Dialog currentDialog) {
        DialogUtilv7.currentDialog = currentDialog;
        return currentDialog;
    }

    public static class DialogClicker implements
            DialogInterface.OnClickListener {

        public void onClick(DialogInterface dialog, int arg1) {
            dialog.cancel();
            dialog.dismiss();
        }

    }

    public static final int loading = 0;

    public static class DialogInfo {
        public String title;
        public int iconId;
        public SpannableStringBuilder message;
        public String positiveButtonText;
        public DialogInterface.OnClickListener positiveButtonClickListener;
        public String negativeButtonText;
        public DialogInterface.OnClickListener negativeButtonClickListener;
        public String neutralButtonText;
        public DialogInterface.OnClickListener neutralButtonClickListener;
        public View view;

        public Context aty;

        public DialogInfo(Context context) {
            super();
            this.aty = context;
        }

    }

    public static DialogInterface.OnClickListener getNewCancelOption(
            Context activity) {
        return getNewCancelOption(activity, false);
    }

    public static DialogInterface.OnClickListener getNewCancelOption(
            final Context activity, final boolean destroyActivity) {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                dialog.dismiss();

            }
        };
    }

    public static AlertDialog showChoiceDialog(DialogInfo abtract) {

        AlertDialog dialog = showChoiceDialog(abtract, true);
        return dialog;
    }

    public static AlertDialog showChoiceDialog(DialogInfo abtract,
                                               boolean cancelable) {
        if (abtract.aty == null) {
            throw new IllegalStateException("contxet is null");
        }
        AlertDialog.Builder dialog = new AlertDialog.Builder(abtract.aty)
                .setCancelable(cancelable);
        if (abtract.positiveButtonText != null
                && abtract.positiveButtonClickListener != null) {
            dialog.setPositiveButton(abtract.positiveButtonText,

                    abtract.positiveButtonClickListener);

        }
        if (abtract.negativeButtonText != null
                && abtract.negativeButtonClickListener != null) {
            dialog.setNegativeButton(abtract.negativeButtonText,
                    abtract.negativeButtonClickListener);

        }

        if (abtract.neutralButtonText != null
                && abtract.neutralButtonClickListener != null) {
            dialog.setNeutralButton(abtract.neutralButtonText,
                    abtract.neutralButtonClickListener);

        }

        if (abtract.title != null) {
            dialog.setTitle(abtract.title);
        }
        if (abtract.iconId != 0) {
            dialog.setIcon(abtract.iconId);
        }
        if (abtract.message != null) {
            dialog.setMessage(abtract.message);
        }
        if (abtract.view != null) {
            dialog.setView(abtract.view);
        }
        AlertDialog ad = dialog.create();
        ad.show();
        return ad;
    }

    public static AlertDialog showNeutralDialog(DialogInfo abtract) {
        return showNeutralDialog(abtract, false);
    }

    public static AlertDialog showNeutralDialog(DialogInfo abtract,
                                                boolean cancable) {
        return new AlertDialog.Builder(abtract.aty)
                .setTitle(abtract.title)
                .setIcon(abtract.iconId)
                .setMessage(abtract.message)
                .setNeutralButton(abtract.neutralButtonText,
                        abtract.neutralButtonClickListener)
                .setCancelable(cancable).setView(abtract.view).show();
    }

    public void closeHintNBackToLauncher(DialogInterface dialog, Context context) {
        dialog.dismiss();
        dialog.cancel();
        ((Activity) context).finish();
    }
}
